package com.aisile.sellergoods.service.impl;

import com.aisile.entity.PageResult;
import com.aisile.mapper.TbSpecificationMapper;
import com.aisile.mapper.TbSpecificationOptionMapper;
import com.aisile.pojo.TbSpecification;
import com.aisile.pojo.TbSpecificationExample;
import com.aisile.pojo.TbSpecificationOption;
import com.aisile.pojo.TbSpecificationOptionExample;
import com.aisile.pojogroup.Specification;
import com.aisile.sellergoods.service.SpecificationService;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;


@Service
public class SpecificationServiceImpl implements SpecificationService {
    @Autowired
    TbSpecificationMapper tbSpecificationMapper;
    @Autowired
    TbSpecificationOptionMapper tbSpecificationOptionMapper;

    @Override
    public PageResult findPage(int page, int rows, TbSpecification tbSpecification) {
        PageHelper.startPage(page,rows);
        String specName = tbSpecification.getSpecName();
        TbSpecificationExample tbSpecificationExample = new TbSpecificationExample();
        if (!StringUtils.isEmpty(specName)){
            tbSpecificationExample.createCriteria().andSpecNameLike("%"+specName+"%");

        }
        Page<TbSpecification> pageModel = (Page<TbSpecification>)tbSpecificationMapper.selectByExample(tbSpecificationExample);
        return new PageResult(pageModel.getTotal(),pageModel.getResult());
    }

    @Override
    public void save(Specification specification) {
        TbSpecification spec = specification.getSpec();
        List<TbSpecificationOption> specOptionList = specification.getSpecOptionList();
        tbSpecificationMapper.insert(spec);
        if (specOptionList!=null && specOptionList.size()>0){
            for (TbSpecificationOption specOption :specOptionList){
                specOption.setSpecId(spec.getId());
                tbSpecificationOptionMapper.insert(specOption);
            }
        }

    }

    @Override
    public Specification findOne(Long id) {
        //根据id查询规格主表数据
        TbSpecification tbSpecification = tbSpecificationMapper.selectByPrimaryKey(id);
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        example.createCriteria().andSpecIdEqualTo(id);
        List<TbSpecificationOption> tbSpecificationOptions = tbSpecificationOptionMapper.selectByExample(example);
        return new Specification(tbSpecification,tbSpecificationOptions);
    }

    @Override
    public void update(Specification specification) {
        TbSpecification spec = specification.getSpec();
        List<TbSpecificationOption> specOptionList = specification.getSpecOptionList();
        //更新spec信息
        tbSpecificationMapper.updateByPrimaryKey(spec);
        //根据specid进行删除
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        example.createCriteria().andSpecIdEqualTo(spec.getId());
        tbSpecificationOptionMapper.deleteByExample(example);
        //再添加规格选项集合
        if (specOptionList!=null && specOptionList.size()>0){
            for (TbSpecificationOption tbSpecificationOption : specOptionList){
                tbSpecificationOption.setSpecId(spec.getId());
                tbSpecificationOptionMapper.insert(tbSpecificationOption);
            }
        }
    }

    @Override
    public void dele(Long[] ids) {
        if (ids!=null && ids.length>0){
            for (Long id : ids){
                tbSpecificationMapper.deleteByPrimaryKey(id);
                TbSpecificationOptionExample example = new TbSpecificationOptionExample();
                example.createCriteria().andSpecIdEqualTo(id);
                tbSpecificationOptionMapper.deleteByExample(example);
            }
        }
    }

    @Override
    public List<Map> findSpecList() {

        return tbSpecificationMapper.findSpecList();
    }

}
